#ifndef PHYLINK_H
#define PHYLINK_H

/**
 * @copyright Copyright (c) 2025 
 * CSG Digital Grid Technology (Guangdong) Co., Ltd.
 * @author zhouyan
 * @brief 设备管理类
*/
#include <cstdint>
#include <string>

struct UartSettings {
    uint32_t baudrate;      // 波特率 9600 115200
    char parity;            // 奇偶校验位 N E O
    std::string paritystr_;
};

struct TcpSettings {
    std::string ip;
    uint32_t port;
};

struct Iec101Settings {
    uint8_t cot_len;
    uint8_t coa_len;
    uint8_t info_obj_addr_len;
    uint8_t link_addr_len;

    bool reversal_fcb_after_link_lost;
    bool ignore_quality;
    uint32_t gen_again_time_ms;
};

typedef struct TagComSettings {
    std::string dev_name;
    std::string uart_name;
    std::string mode;
    std::string app_name;
    bool is_uart;
    bool dump_msg;
    TcpSettings tcp;
    UartSettings uart;
    uint32_t recv_frame_timeout_ms;
    uint32_t recv_char_timeout_ms;
    uint32_t retry_cnt_at_recv_timeout;
    uint32_t poll_call_wait_time_ms;
    uint32_t ignore_call_cnt_after_link_lost;
    uint32_t cmd_interval_ms;
    Iec101Settings iec101;
}ComSettings;


class IPhyLink
{
protected:
    IPhyLink();
public:
    virtual ~IPhyLink();

    static IPhyLink *createDev(ComSettings *settings);

    const ComSettings * comSettings(void) const {return settings_; }
    ComSettings * comSettings(void) {return settings_; }

    const char *devName(void) const {return settings_->dev_name.c_str();}
    const char *UartName(void) const {return settings_->uart_name.c_str();}
    bool dumpMsge(void) const {return settings_->dump_msg;}

    virtual bool isActived(void) const = 0;
    virtual bool connect(int timeout) = 0;
    virtual void disconnect(void) = 0;

    virtual uint32_t read(void *buf, uint32_t max_byte);
    virtual uint32_t read(void *buf, uint32_t max_byte, uint32_t timeout) = 0;
    virtual uint32_t write(const void *buf, uint32_t max_byte) = 0;
    virtual void clear(void) = 0;
    virtual void restore(void) {};

    virtual void lock(void) = 0;
    virtual void unlock(void) = 0;
    virtual bool tryLock(void) = 0;
    virtual bool SwitchSettings(ComSettings *settings_, bool isReopen = true) = 0;
    virtual int async_read(uint8_t *RecvBuf, uint32_t max_byte, uint32_t i_RecvTimeoutS) = 0;
protected:
    ComSettings *settings_;
};

#endif // PHYLINK_H
